/**
 * Created by chenshengli on 2015/7/9 0009.
 */
var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
var Utils = require('./utils');

var mongoDB = null;

var mongoURL = "mongodb://127.0.0.1:27017/order";

MongoClient.connect(mongoURL, function (err, db) {
    if (err)console.error("mongodb init error", err);
    mongoDB = db;
    console.log("mongodb init~");
});

router.get("/", function (req, res) {
    res.render("history");
})


router.get("/query", function (req, res) {
    var startStr = req.param("start");
    var endStr = req.param("end");
    var start =Utils.stringToDate(startStr);
    var end = Utils.stringToDate(endStr);
    //start.setDate(start.getDate()-7);
    //var end = new Date();
    end.setDate(end.getDate()+1);
    console.log(start.toLocaleDateString());
    console.log(end.toLocaleDateString());
    //mongoDB.collection("orders").find({"$and":[{"time":{"$gte":start}},{"time":{"$lt":end}}], show: "1"}, {sort: {'name': 1}}).toArray(function (err, docs) {
    mongoDB.collection("orders").find({"orderDate":{"$gte":start,"$lt":end}, show: "1"}, {sort: {'name': 1}}).toArray(function (err, docs) {
        if (err) {
            res.send([]);
        } else {
            res.send(docs);
        }
    });
})

module.exports = router;